/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.msg.hrappy.web.servlet;

import edu.msg.hrappy.backend.model.Job;
import edu.msg.hrappy.backend.model.User;
import edu.msg.hrappy.backend.service.JobService;
import edu.msg.hrappy.backend.service.UserService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author toroke
 */
public class ShowAllJobsServlet extends HttpServlet {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    @EJB
    private JobService jobService;
    @EJB
    private UserService userService;

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        System.out.println("request");
        HttpSession session = req.getSession();

        List<Job> allJobs = jobService.getAll();

        List<Job> jobs = new ArrayList<Job>();
        List<Job> njobs = new ArrayList<Job>();
        for (Job j : allJobs) {
            if (j.isActive()) {
                jobs.add(j);
            } else {
                njobs.add(j);
            }
        }
        session.setAttribute("er", null);
        session.setAttribute("jobs", jobs);
        session.setAttribute("njobs", njobs);
        User user = userService.getUserByUserName(req.getUserPrincipal()
                .toString());
        System.out.println(user.getRoleInCompany());
        session.setAttribute("role", user.getRoleInCompany());
        session.setAttribute("username", user.getUserName());
        String address = "./showAllJobs.jsp";

        RequestDispatcher dispatcher = req.getRequestDispatcher(address);

        dispatcher.forward(req, resp);

    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {

        Long id = Long.parseLong(req.getParameter("jobId"));

        Job job = jobService.getById(id);
        job.setActive(true);
        job.setOpen(true);
        if (job.getNrOfPositions() == 0) {
            job.setNrOfPositions(1);
        }
        jobService.update(job);
        HttpSession session = req.getSession();

        List<Job> allJobs = jobService.getAll();

        List<Job> jobs = new ArrayList<Job>();
        List<Job> njobs = new ArrayList<Job>();
        for (Job j : allJobs) {
            if (j.isActive()) {
                jobs.add(j);
            } else {
                njobs.add(j);
            }
        }
        session.setAttribute("jobs", jobs);
        session.setAttribute("njobs", njobs);
        session.setAttribute("er", null);
        User user = userService.getUserByUserName(req.getUserPrincipal()
                .toString());
        System.out.println(user.getRoleInCompany());
        session.setAttribute("role", user.getRoleInCompany());
        session.setAttribute("username", user.getUserName());
        String address = "./showAllJobs.jsp";

        RequestDispatcher dispatcher = req.getRequestDispatcher(address);

        dispatcher.forward(req, resp);
    }
}
